<?php 
if (!isset($no_session_start))
	@session_start();

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '2001105330');
define('DB_NAME', 'javnow'); 
define('SITE', '娇娃');

function conn()
{
    $conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_NAME, $conn);
    return $conn;
}

function paginate($total, $curr_page_num, $link) {
    //$curr_page_num 当前页数
    //$link 链接的页面，比如index.php，则$link=index
    if($curr_page_num == 1) {
        $prev = 1; //如果当前页为第1页，那么点上1页的时候，显示的也是第1页
    }
    else {
        $prev = $curr_page_num - 1;    //否则的话，上1页为：当前页-1
    }
    
    if($curr_page_num == $total)
        $next = $total;//如果当前页为最后1页，那么点下1页的时候，显示的也是最后1页
    else 
        $next = $curr_page_num+1;//否则的话，下1页为：当前页+1
    
    $str = '';
    
    if(1 != $curr_page_num) {        
        $str .= "<a class=\"p_n\" href=\"/$link/$prev/\">上一页</a>";//链接为$link?p=上1页
    }
    
    $str .= "&nbsp;";
    
    $first = $curr_page_num <= 4 ? 1 : $curr_page_num - 4;
    $last = $curr_page_num + 5 > $total ? $total : $curr_page_num + 5;
    for($i=$first;$i<=$last;$i++) {
        if($curr_page_num != $i) {
            $str .= "<a href=\"/$link/$i/\">$i</a>";//生成中间的 1  2  3  4  5 并设置链接
        }
        else {
            $str .= "<a class=\"no_bgb\">".$i."</a>";
        }
        
        $str .= "&nbsp;";
        
    }
    
    if($curr_page_num != $total) {
        $str .= "<a class=\"p_n\" href=\"/$link/$next/\">下一页</a>";//链接为$link?p=下1页
    }
    
    return $str;
}

function read_ok(&$sock){
    $r = '';
    do {
	$r .= fread($sock,1000);
	$s = @socket_get_status($sock);
    } while ($s['unread_bytes']);
    if (!preg_match("/^[23]/", $r)) { 
	fputs($sock,"QUIT\r\n");
	return false;
    }
    return true;
}

function send($to, $toname, $subject, $html, $text, $from, $fromname, $attachment_path, $attachment_name) {
    $timeout = 60;
    $boundary = uniqid("");
    $boundary_internal = uniqid("=");
    $sock = @fsockopen("***.***.***", 25, $errno, $errstr, $timeout); 
    if(!read_ok($sock))return false;
    fputs($sock,"EHLO ***.com\r\n");
    if(!read_ok($sock))return false;
    fputs($sock,"AUTH LOGIN\r\n");
    if(!read_ok($sock))return ;
    fputs($sock,base64_encode("USERNAME")."\r\n");
    if(!read_ok($sock))return ;
    fputs($sock,base64_encode("PASSWORD")."\r\n");
    if(!read_ok($sock))return ;
    fputs($sock,"MAIL FROM:no-reply@***.com\r\n");
    if(!read_ok($sock))return false;
    fputs($sock,"RCPT TO:".$to."\r\n");
    if(!read_ok($sock))return false;
    fputs($sock,"DATA\r\n");
    if(!read_ok($sock))return false;
    fputs($sock,"From: ".(isset($from)&&isset($fromname)&&!empty($from)&&!empty($fromname)?"$fromname <$from>":"no-reply <no-reply@***.com>")."\r\n");
    fputs($sock,"To: ".(isset($toname)&&!empty($toname)?$toname." <$to>":$to)."\r\n");
    fputs($sock,"Subject: ".$subject."\r\n");
    fputs($sock,"MIME-Version:1.0\r\n");
    if(!isset($attachment_name) || '' == $attachment_name) {
		fputs($sock,"Content-Type:multipart/alternative; boundary=\"$boundary\"\r\n");
    }
    else {
		fputs($sock,"Content-Type:multipart/mixed; boundary=\"$boundary\"\r\n");
    }
    fputs($sock, "--$boundary\r\n");
    if(!isset($attachment_name) || '' == $attachment_name) {
		fputs($sock, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
		fputs($sock, "Content-Transfer-Encoding: base64\r\n\r\n");
		fputs($sock, chunk_split(base64_encode($text), 72)."\r\n\r\n");
		fputs($sock, "--$boundary\r\n");
		fputs($sock, "Content-type:text/html; charset=\"iso-8859-1\"\r\n");
		fputs($sock, "Content-transfer-encoding:base64\r\n\r\n");
		fputs($sock, chunk_split(base64_encode($html), 72)."\r\n\r\n");
    }
    else {
		fputs($sock, "Content-Type: multipart/alternative; boundary=\"$boundary_internal\"\r\n\r\n");
		fputs($sock, "--$boundary_internal\r\n");
		fputs($sock, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
		fputs($sock, "Content-Transfer-Encoding: base64\r\n\r\n");
		fputs($sock, chunk_split(base64_encode($text), 72)."\r\n\r\n");
		fputs($sock, "--$boundary_internal\r\n");
		fputs($sock, "Content-type:text/html; charset=\"iso-8859-1\"\r\n");
		fputs($sock, "Content-transfer-encoding:base64\r\n\r\n");
		fputs($sock, chunk_split(base64_encode($html), 72)."\r\n\r\n");
		fputs($sock, "--".$boundary_internal."--\r\n\r\n");
		fputs($sock, "--".$boundary."\r\n");
		fputs($sock, "Content-type:application/octet-stream; charset=\"iso-8859-1\"; name=".$attachment_name."\r\n");
		fputs($sock, "Content-Disposition:attachment; filename=".$attachment_name."\r\n");
		fputs($sock, "Content-Transfer-Encoding:base64\r\n\r\n");
		$content = file_get_contents($attachment_path);
		fputs($sock, chunk_split(base64_encode($content), 72)."\r\n\r\n");
    }
    
    fputs($sock, "--".$boundary."--\r\n");
    fputs($sock,"\r\n.\r\n");
    fputs($sock,"QUIT\r\n");
    
    return true;
}

// 如果购买的时间过期了，就返回false；否则返回true
function check_permission() 
{
	$account_id = $_SESSION['id'];
	$conn = conn();
	$sql = "SELECT UNIX_TIMESTAMP(created) created, name good FROM pay_history JOIN goods ON good_id = goods.id WHERE account_id = $account_id ORDER BY created";
	$result = mysql_query($sql, $conn);
	$expiration = null;
	while ($row = mysql_fetch_assoc($result))
	{
		if (null == $expiration || $expiration <= $row['created'])
		{
			$expiration = strtotime('+'.$row['good'], $row['created']);
		}
		else
		{
			$expiration = strtotime('+'.$row['good'], $expiration);
		}
	}
	if (null == $expiration || $expiration < time())
		return false;
	else 
		return true;
}
?>